package com.dartfar.base.service;

import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.dartfar.base.dao.DaoUtil;
import com.dartfar.base.vo.TreeVo;
import com.dartfar.organize.vo.OrganizeTreeVo;
import com.dartfar.util.StringUtils;

/**
 * 通用ServiceImpl 标准CRUD
 * @author 王琦
 */
public class BaseServiceImpl<T> implements BaseService<T> {

	private static final Logger logger = Logger.getLogger(BaseServiceImpl.class);
	
	@Resource
	private DaoUtil<T> baseDaoUtil;

	private Class<T> persistentClass;

	public BaseServiceImpl() {
		this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
	}
	
	public Class<T> getPersistentClass() {
		return persistentClass;
	}
	
	/**
	 * 保存
	 * @param t 对象
	 * @return 对象ID
	 */
	@Override
	public int save(T t) {
		// TODO Auto-generated method stub
		return baseDaoUtil.save(t);
	}

	/**
	 * 获取对象
	 * @param id ID
	 * @return 对象
	 */
	@Override
	public T get(int id){
		// TODO Auto-generated method stub
		return baseDaoUtil.get(id, this.persistentClass);
	}
	
	/**
	 * 获取全部列表
	 * @return 列表
	 */
	@Override
	public List<T> getList(){
		String hql = "from " + this.persistentClass.getName();
		return baseDaoUtil.getList(hql);
	}
	
	/**
	 * 根据hql语句获取列表
	 * @param hql
	 * @return
	 */
	@Override
	public List<T> getList(String hql){
		return baseDaoUtil.getList(hql);
	}
	
	/**
	 * 获取全部列表带分页
	 * @param pageNo pageSize
	 * @return 列表
	 */
	@Override
	public List<T> getListForPage(int pageNo, int pageSize){
		String hql = "from " + this.persistentClass.getName();
		return baseDaoUtil.getListForPage(hql, (pageNo-1)*pageSize, pageSize);
	}
	
	/**
	 * 获取全部列表记录总数
	 * @return 数量
	 */
	public Long getCount(){
		String hql = "from " + this.persistentClass.getName();
		return baseDaoUtil.getCount(hql);
	}
	
	/**
	 * 修改对象
	 * @param t 对象
	 */
	@Override
	public void update(T t) {
		// TODO Auto-generated method stub
		baseDaoUtil.update(t);
	}

	
	/**
	 * 删除对象
	 * @param t 对象
	 */
	@Override
	public void delete(T t) {
		// TODO Auto-generated method stub
		baseDaoUtil.delete(t);
	}


	@Override
	public void execute(String hql) {
		// TODO Auto-generated method stub
		baseDaoUtil.execute(hql);
	}	

	@Override
	public TreeVo buildJSONTreeNode(List<TreeVo> lists) {
		// TODO Auto-generated method stub
		
		logger.info("lists : " + lists.size());
		
		TreeVo tree = new TreeVo();
		for(TreeVo node : lists) { //当前循环这个集合每一个元素
			logger.info("AAA：" + JSONObject.fromObject(node).toString());

			//if(!StringUtils.isNotEmpty(node.getParent())){
			if(node.getParent().equalsIgnoreCase("0")){
				tree = node;
				
				lists.remove(node);
				break;
			}
		}
		buildJSONTreeNodeChildren(lists, tree);
		
		logger.info("tree : " + tree.toString());
		
		return tree;
	}
	
	public void buildJSONTreeNodeChildren(List<TreeVo> childrens, TreeVo root){
		String parent = root.getId();
		for(int i = 0; i < childrens.size(); i++){
			TreeVo node = childrens.get(i);
			if(node.getParent().equalsIgnoreCase(parent)){
				root.getChildren().add(node);
				childrens.remove(i);
				i = i - 1;
				buildJSONTreeNodeChildren(childrens, node);
			}
			if(i == childrens.size()-1){
				return;
			}
		}
	}

}
